package com.itheima.aspect;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.RedisConstant;
import com.itheima.service.ApiCountService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Date;

@Component
@Aspect
@Order(0)
public class MyAspect {
/*    @Autowired
    private JedisPool jedisPool;*/

    @Reference
    private ApiCountService apiCountService;


    @Around("execution(* com.itheima.controller.CheckItemController.*(..))")
    public Object around1(ProceedingJoinPoint pj) {
        Object proceed = null;
        //获取目标对象的方法名
        try {
            proceed = pj.proceed();
            Signature signature = pj.getSignature();
            if (signature.getName().equals("findPage")) {
                saveApiCount4Redis(RedisConstant.API_CHECKITEM_QUERY);
            } else if (signature.getName().equals("edit")) {
                saveApiCount4Redis(RedisConstant.API_CHECKITEM_EDIT);
            } else if (signature.getName().equals("add")) {
                saveApiCount4Redis(RedisConstant.API_CHECKITEM_ADD);
            } else if (signature.getName().equals("delete")) {
                saveApiCount4Redis(RedisConstant.API_CHECKITEM_DELETE);
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return proceed;
    }


    @Around("execution(* com.itheima.controller.CheckGroupController.*(..))")
    public Object around2(ProceedingJoinPoint pj) {
        Object proceed = null;
        //获取目标对象的方法名
        try {
            proceed = pj.proceed();
            Signature signature = pj.getSignature();
            if (signature.getName().equals("findPage")) {
                saveApiCount4Redis(RedisConstant.API_CHECKGROUP_QUERY);
            } else if (signature.getName().equals("edit")) {
                saveApiCount4Redis(RedisConstant.API_CHECKGROUP_EDIT);
            } else if (signature.getName().equals("add")) {
                saveApiCount4Redis(RedisConstant.API_CHECKGROUP_ADD);
            } else if (signature.getName().equals("delete")) {
                saveApiCount4Redis(RedisConstant.API_CHECKGROUP_DELETE);
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return proceed;
    }


    private void saveApiCount4Redis(String apiName) {
/*        Jedis jedis = jedisPool.getResource();
        Long time = new Date().getTime();
        Double oldScore = jedis.zscore(RedisConstant.API_COUNT, apiName);
        Double newScore = null;
        if (oldScore == null) {
            newScore = Double.parseDouble(1 + "." + time);
        } else {
            newScore = Double.parseDouble((int) Math.floor(oldScore) + 1 + "." + time);
        }
        //存储到redis中
        jedis.zadd(RedisConstant.API_COUNT, newScore, apiName);*/
        //存储到DB中
        try {
            apiCountService.saveCount4DB(apiName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
